/**
 * 数据库操作
 */

const { execSql } = require('../exec/execSql')
const { getPassword } = require('../util/getPassword')

const getList = (keyword) => {
    let sql = `select * from users where 1=1 `

    if (keyword) {
        sql += `and username like '%${keyword}%' `
    }
    sql += `order by createtime desc;`
    return execSql(sql)
}

const getDetail = (id) => {
    let sql = `select * from users where id='${id}';`
    return execSql(sql)
}

const register = (username, password, realname, createtime, updatetime) => {
    password = getPassword(password)
    let sql = `insert into users(username, password, realname, createtime, updatetime) values('${username}', '${password}', '${realname}', '${createtime}', '${updatetime}');`
    return execSql(sql)
}

const editUser = (username, password, realname, updatetime, id) => {
    password = getPassword(password)
    let sql = `update users set username='${username}', password='${password}', realname='${realname}', updatetime='${updatetime}' where id='${id}';`
    return execSql(sql)
}

const deleteUser = (id) => {
    let sql = `delete from users where id='${id}';`
    return execSql(sql)
}

const login = (username, password) => {
    password = getPassword(password)
    let sql = `select * from users where username='${username}' and password='${password}';`
    return execSql(sql)
}


module.exports = {
    getList,
    getDetail,
    register,
    editUser,
    deleteUser,
    login
}